//app.js
App({
    onLaunch: function (e) {
        var that = this;
        let userInfo = wx.getStorageSync('userInfo');
        if (!userInfo) {
            this.util.getUserInfo();
        }
        this.initPage();
    },
    onShow: function (e) {
        if (e.query.share_user_id) {
            wx.setStorageSync('share_user_id', e.query.share_user_id);
        }
    },
    initPage: function (siteData) {
        this.setSiteData(siteData);
    },
    setSiteData: function (siteData) {
        var that = this;
        this.siteInfo.sitepath = this.siteInfo.siteroot.slice(0, -13);
        //获取站点信息
        this.util.app = this;
        this.util.request({
            'url': 'index/siteInfo',
            'cachetime': '600',
            success: function (res) {
                var data = res.data.data;
                that.globalData.siteInfo = data;
                that.siteInfo.siteData = data;

                if (siteData && siteData.navigationBarTitleText != undefined) {
                    wx.setNavigationBarTitle({
                        title: siteData.navigationBarTitleText,
                    })
                } else {
                    if (data.navigationBarTitleText) {
                        wx.setNavigationBarTitle({
                            title: data.navigationBarTitleText,
                        })
                    }
                }

                //顶部导航
                if (data && data.data.navigationBarColor) {
                    //动画
                    if (data.data.navigationBarColor.animation.enable == 1) {
                        wx.setNavigationBarColor({
                            frontColor: data.data.navigationBarColor.frontColor,
                            backgroundColor: data.data.navigationBarColor.backgroundColor,
                            animation: {
                                duration: data.data.navigationBarColor.animation.duration,
                                timingFunc: data.data.navigationBarColor.animation.timingFunc
                            }
                        });
                    } else {
                        wx.setNavigationBarColor({
                            frontColor: data.data.navigationBarColor.frontColor,
                            backgroundColor: data.data.navigationBarColor.backgroundColor,
                        });
                    }
                }

                //设置底部导航样式
                if (data && data.data.tabBar) {
                    that.tabBar.backgroundColor = data.data.tabBar.backgroundColor;
                    that.tabBar.selectedColor = data.data.tabBar.selectedColor;
                    that.tabBar.color = data.data.tabBar.color;
                    that.tabBar.borderStyle = data.data.tabBar.borderStyle;
                }

                //底部导航列表
                if (data && data.data.tabBarList && data.data.tabBarList.length > 0) {
                    that.tabBar.list = data && data.data.tabBarList;
                }
            }
        });
    },
    onHide: function () {

    },
    onError: function (msg) {
        console.log(msg);
    },
    onPageNotFound: function () {
    },
    "tabBar": {
        "list": [
          {
            "pagePath": "/wxz_superstore/pages/newindex/newindex",
            "iconPath": "/wxz_superstore/resource/images/own-icon.png",
            "selectedIconPath": "/wxz_superstore/resource/images/own-selected-icon.png",
            "text": "首页"
          },
            {
                "pagePath": "/wxz_superstore/pages/zbDetail/zbDetail",
                "iconPath": "/wxz_superstore/resource/images/home-icon.png",
                "selectedIconPath": "/wxz_superstore/resource/images/home-selected-icon.png",
                "text": "直播"
            },

            {
                "pagePath": "/wxz_superstore/pages/person-info/person-info",
                "iconPath": "/wxz_superstore/resource/images/own-icon.png",
                "selectedIconPath": "/wxz_superstore/resource/images/own-selected-icon.png",
                "text": "个人中心"
            }
        ],
        "color": "#515151",
        "selectedColor": "#ff5777",
        "backgroundColor": "#fff",
        "borderStyle": "white"
    },
    getUserInfo: function (cb) {
        var that = this
        if (this.globalData.userInfo) {
            typeof cb == "function" && cb(this.globalData.userInfo)
        } else {
            //调用登录接口
            wx.login({
                success: function () {
                    wx.getUserInfo({
                        success: function (res) {
                            that.globalData.userInfo = res.userInfo
                            typeof cb == "function" && cb(that.globalData.userInfo)
                        }
                    })
                }
            })
        }
    },
    globalData: {
        userInfo: null,
    },
    util: require('we7/resource/js/util.js'),
    siteInfo: require('siteinfo.js')
})